#include "melee.hpp"

namespace gomoku{

    melee::melee(int height, int width):player_vs_player{height,width}{
        for (int i=0;i<height;++i){
            for (int j=0;j<width;++j){
                if ((i+j)%2==0){
                    this->board.disable_point({i,j});
                }
            }
        }
    }

    melee::melee(int height, int width,int init_status, point_status *init_layout):player_vs_player{height,width,init_status,init_layout}{

    }

    bool melee::do_operation(point position){
        /* melee类的status对应值
        **0:黑方进行操作
        **1:白方进行操作
        **2:黑方启用第二个方格
        **3:白方启用第二个方格
        */
       switch (_status){
            case 0:
                switch (this->board[position]){
                    case point_status::black:
                        return false;
                        break;
                    case point_status::vacant:
                        this->board.place_black(position);
                        _status = 1;
                        _undone = false;
                        break;
                    case point_status::disabled:
                        this->board.clear_point(position);
                        _status = 2;
                        _undone = false;
                        break;
                }
                break;
            case 1:
                switch (this->board[position]){
                    case point_status::white:
                        return false;
                        break;
                    case point_status::vacant:
                        this->board.place_white(position);
                        _status = 0;
                        _undone = false;
                        break;
                    case point_status::disabled:
                        this->board.clear_point(position);
                        _status = 3;
                        _undone = false;
                        break;
                }
                break;
            case 2:
                if (this->board[position]==point_status::disabled){
                    this->board.clear_point(position);
                    _status = 1;
                    _undone = false;
                }else{
                    return false;
                }
                break;
            case 3:
                if (this->board[position]==point_status::disabled){
                    this->board.clear_point(position);
                    _status = 0;
                    _undone = false;
                }else{
                    return false;
                }
                break;
       }
       return true;
    }

    bool melee::undo_operation(){
        if (_undone || this->board.change_log.empty() || _status==2 || _status==3){
            return false;
        }else{
            auto p=this->board.change_log.rbegin();
            switch(p->before){
                case point_status::vacant:
                    this->board.clear_point(p->pos);
                    break;
                case point_status::disabled:
                    this->board.disable_point(p->pos);
                    this->board.disable_point((p+1)->pos);
                    break;
            }
            _undone = true;
            ++_status;
            _status %= 2;
            return true;
        }
    }

}